#Copyright 2011 Wesley Crossman - No warranty - Placed under GPLv3 (or later, your choice)
#Arguments: updateinfo.py <version> <copyright year range>
#Example: updateinfo.py 1.0 2011-2012
#
#Assumptions:
#Current path is c:\dev\steeptimer\source
#Compiled release version of program is at c:\dev\SteepTimer-build-desktop\release
#   (to make this easy, compile with Qt Creator)
#InnoSetup compiler in path
#Collection of files to include exists at c:\dev\SteepTimer-build-desktop\steeptimer:
#   * imageformats\qico4.dll
#   * phonon_backend\phonon_ds94.dll
#   * sounds\{clips.csv,*.mp3}
#   * leaf.ico
#   * LICENSE.txt
#   * msvc[rp]100.dll
#   * Qt{Core,Gui}4.dll
#   * vcredist_x86.exe
#   * SteepTimer.exe (automatically copied from SteepTimer-build-desktop\release)
#
#Changes:
#Creates steeptimer-VERSION.zip, steeptimer-src-VERSION.zip, SetupSteepTimer.exe

import sys, os, shutil, zipfile

def zip_folder(path, zip_root, fname, excludeList, excludeDotted):
    with zipfile.ZipFile(fname, 'w', compression=zipfile.ZIP_DEFLATED) as myzip:
        root_len = len(os.path.abspath(path))
        for root, dirs, files in os.walk(path):
            archive_root = os.path.join(zip_root, os.path.abspath(root)[root_len+1:])
            for f in files:
                fullpath = os.path.join(root, f)
                archive_name = os.path.join(archive_root, f)
                if f not in excludeList and (not excludeDotted or root.find("\\.") == -1):
                    myzip.write(fullpath, archive_name, zipfile.ZIP_DEFLATED)

def main():
    #Prep
    os.chdir("../../SteepTimer-build-desktop")
    shutil.copyfile("release/SteepTimer.exe", "steeptimer/SteepTimer.exe")
    version = open("../steeptimer/version.txt", "r").read()

    #Binary ZIP
    zip_folder("steeptimer",
               "SteepTimer " + version,
               "steeptimer-" + version + ".zip",
               ["vcredist_x86.exe"], #This installer is not needed, since the ZIP uses the bundled DLLs
               True)

    #Source ZIP
    os.chdir("..")
    zip_folder("steeptimer",
               "steeptimer-src-" + version,
               "SteepTimer-build-desktop/steeptimer-src-" + version + ".zip",
               ["version.txt"], #used for this script, added by updateinfo.py
               True)
    os.chdir("SteepTimer-build-desktop")

    #Installer
    ret = os.system(r"iscc /q ..\steeptimer\source\setup.iss")
    assert(ret == 0)

main()
